package com.news.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

public class CheckCode_02 {
	
	/**

     * 测试

     */

    public static void main(String args[]) {

       test();

    }

 

    static void test() {

       // 验证码图片的宽度。

       final int width = 80;

       // 验证码图片的高度。

       final int height = 25;

       // 校验码的大小

       final int SIZE = 4;

 

       String checkCode = getCheckCode(SIZE);

       BufferedImage bi = CheckCode_02.createCheckCodeImage(width, height,

              checkCode);

       File checkFile = null;

       checkFile = new File("D:/temp/check.jpg");

       try {

           ImageIO.write(bi, "jpeg", checkFile);

       } catch (IOException e) {

           e.printStackTrace();

       }

    }

 

    private static final String CHECK_CODE[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

 

    /**

     * 获得校验码<br>

     * 校验码的内容：大小写字母 + 数字

     * 

     * @param SIZE

     *            校验码的个数

     * @return 校验码

     */

    public static String getCheckCode(final int SIZE) {

       Random random = new Random();

       StringBuilder rands = new StringBuilder();

       int r;

       int len = CHECK_CODE.length;

       for (int i = 0; i < SIZE; i++) {

           r = random.nextInt(len);

           rands.append(CHECK_CODE[r]);

       }

       return rands.toString();

    }

 

    /**

     * 获得校验码图片<br>

     * <br>

     * 字体的大小应该根据图片的高度来定。<br>

     * <code>

     * Font font = new Font("Times New Roman", Font.BOLD, height);

     * </code>

     * 

     * @param width

     *            图片宽度

     * @param height

     *            图片高度

     * @param checkCode

     *            校验码

     * @return 校验码图像数据缓冲

     */

    public static BufferedImage createCheckCodeImage(int width, int height, String checkCode) {

       BufferedImage bi = new BufferedImage(width, height,

              BufferedImage.TYPE_INT_RGB);

       Graphics2D g = bi.createGraphics();

 

       g.setColor(Color.WHITE);

       g.fillRect(0, 0, width, height);

 

       // 创建字体，字体的大小应该根据图片的高度来定。

       Font font = new Font("Times New Roman", Font.BOLD | Font.ITALIC, height);

       // 设置字体。

       g.setFont(font);

 

       // 画边框。

       g.setColor(Color.BLACK);

       g.drawRect(0, 0, width - 1, height - 1);


       Random random = new Random();

       // 随机产生条干扰线，使图象中的认证码不易被其它程序探测到。

       g.setColor(getColor(width, height, random));

       int x1, y1, x2, y2;

       for (int i = 0; i < 5 + height; i++) {

           x1 = random.nextInt(width - 5);

           y1 = random.nextInt(height - 5);

           x2 = random.nextInt(12);

           y2 = random.nextInt(12);

           g.drawLine(x1, y1, x1 + x2, y1 + y2);

       }

 

       int fontNum = checkCode.length();

       int fontWidth = (width - (fontNum + 1) * fontNum) / fontNum;

       for (int i = 0; i < checkCode.length(); i++) {

           // 用随机产生的颜色将验证码绘制到图像中。

           g.setColor(getColor(height, width, random));

           g.drawString(checkCode.charAt(i) + "", 4 + (fontWidth + fontNum) * i, height - 4);

       }

       g.dispose();

       return bi;

    }

    private static Color getColor(int i, int j, Random random) {

       // 给定范围获得随机颜色

       if (j < i)

           j += 255;

       if (i > 255)

           i = 255;

       if (j > 255)

           j = 255;

       int n = j - i;

       int r = i + random.nextInt(n);

       int g = i + random.nextInt(n);

       int b = i + random.nextInt(n);

       return new Color(r, g, b);

    }


}
